# *-* coding: utf-8 *-*
from bs4 import BeautifulSoup
import requests

from tools import BaseConfig

def getProxyList():
    session=requests.Session()
    url = 'http://www.ip181.com/daili/%s.html'
    proxy = []
    for i in range(int(1), int(200) + 1):
        req=session.get(url % str(i))
        req.encoding='GBK'
        bs=BeautifulSoup(req.text,'lxml')
        tables = bs.findAll('table')
        tab = tables[0]
        for tr in tab.findAll('tr'):
            i = 0
            tmp = ""
            for td in tr.findAll('td'):
                tmp += td.getText() + ":"
                i += 1
                if (i > 2):
                    if (td.getText() == "高匿" and tmp[:-4] not in proxy):
                        proxy.append(tmp[:-4])
                    break
        if(len(proxy)>=200):
            break;
    return proxy

def getxiciProxyList(proxyList=None,proxyListLen=None,driver=None,proxyNum=0):
    if(proxyNum==0):
        proxyList = getProxyList()
        print(proxyList)
        proxyListLen = len(proxyList)
        driver = BaseConfig.getDriver(proxyList[proxyNum])
        proxyNum += 1
    url="http://www.xicidaili.com/nn/"
    proxy = []
    try:
        driver.get(url)
    except:
        driver.close()
        getContinueProxy(proxyNum,proxyList,proxyListLen)
    else:
        soup = BeautifulSoup(driver.page_source, "lxml")
        titles = soup.find('table', {'id': 'ip_list'})
        if (titles == None or len(titles) < 1):
            driver.close()
            getContinueProxy(proxyNum,proxyList,proxyListLen)
        else:
            for tr in titles.findAll('tr'):
                i=0;tmp=""
                for td in tr.findAll('td'):
                    if (i > 0):
                        tmp+=td.getText()+":"
                        if(i>=2):
                            print(tmp[:-1])
                            proxy.append(tmp[:-1])
                            print(proxy)
                            break
                    i += 1
            return proxy

def getContinueProxy(proxyNum,proxyList,proxyListLen):
    if (proxyListLen > proxyNum-1):
        driver = BaseConfig.getDriver(proxyList[proxyNum])
        proxyNum += 1
        getxiciProxyList(proxyList,proxyListLen,driver, proxyNum)